<?php

namespace App\Imports;

use App\Http\Models\Business\Material;
use App\Http\Models\CategoryBig;
use App\Http\Models\CategoryContract;
use App\Http\Models\CategoryKeyProduct;
use App\Http\Models\CategorySmall;
use Exception;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class MaterialImport implements ToCollection, WithHeadingRow
{
    use BaseImportTrait;

    private $required_field = ['年份', '物料编码', '物料描述', '小分类代码', '大分类代码', '合同分类代码', '重点产品分类代码', '是否特价'];

    private $category_small_id_array = [];
    private $category_big_id_array = [];
    private $category_contract_id_array = [];
    private $category_key_product_id_array = [];

    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            $row = $row->toArray();

            if ($key === 0) {
                $this->verifyField($row);
                $this->prepareBaseData();
            }

            $this->verifyNotNull($row, $key + 2);
            $this->verifyValueExist($row, $key + 2);

            $this->saveData($row);
        }
    }


    //准备基础数据，用来验证
    private function prepareBaseData()
    {
        $this->category_small_id_array = CategorySmall::pluck('id')->toArray();
        $this->category_big_id_array = CategoryBig::pluck('id')->toArray();
        $this->category_contract_id_array = CategoryContract::pluck('id')->toArray();
        $this->category_key_product_id_array = CategoryKeyProduct::pluck('id')->toArray();
    }


    //验证字段值存在于数据库基础表中
    private function verifyValueExist(array $row, int $index)
    {
        if (!in_array($row['小分类代码'], $this->category_small_id_array)) {
            $this->no_error = false;
            $this->error_field = '小分类代码';
        } elseif (!in_array($row['大分类代码'], $this->category_big_id_array)) {
            $this->no_error = false;
            $this->rror_field = '大分类代码';
        } elseif (!in_array($row['合同分类代码'], $this->category_contract_id_array)) {
            $this->no_error = false;
            $this->error_field = '合同分类代码';
        } elseif (!in_array($row['重点产品分类代码'], $this->category_key_product_id_array)) {
            $this->no_error = false;
            $this->error_field = '重点产品分类代码';
        }

        if (!$this->no_error) {
            throw new Exception('第 ' . $index . ' 行中的 [' . $this->error_field . '] 的值不存在于系统中！');
        }
    }

    //保存数据
    private function saveData(array $row)
    {
        Material::updateOrCreate(
            [
                'year' => $row['年份'],
                'material' => $row['物料编码']
            ],
            [
                'material_desc' => $row['物料描述'],
                'category_small_id' => $row['小分类代码'],
                'category_big_id' => $row['大分类代码'],
                'category_contract_id' => $row['合同分类代码'],
                'category_key_product_id' => $row['重点产品分类代码'],
                'is_special_offer' => $row['是否特价'] == '是' ? 1 : 0,
            ]
        );
    }
}
